package com.example.lc.mythreedemo;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.TypeReference;
import com.example.lc.mythreedemo.custom.LazyViewPager;
import com.example.lc.mythreedemo.custom.ViewPageAdapter;

import org.xutils.x;

import java.lang.reflect.Type;
import java.util.List;

public class MainActivity extends FragmentActivity implements ImageCycleView.OnPageItemClickListener, ImageCycleView.OnMoreClickListener {


    private LinearLayout ll_root;
    private TabLayout tl_content;
    private LazyViewPager vp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        x.Ext.init(getApplication());
        x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.

        initView();
        System.out.println(Thread.currentThread().getName() + "主线程运行开始!");
        System.out.println(Thread.currentThread().getName() + "主线程运行结束!");
    }

    private void initView() {
        ll_root = (LinearLayout) findViewById(R.id.ll_root);


        // 初始化数据
        Type type = new TypeReference<ResultList<List<EnterpriseHomeEntity>>>() {
        }.getType();
        ResultList<List<EnterpriseHomeEntity>> parseObject = JsonUtil.fromJson("{\n" +
                "    \"code\":0,\n" +
                "    \"msg\":\"成功\",\n" +
                "    \"en_msg\":\"OK\",\n" +
                "    \"list\":[\n" +
                "        {\n" +
                "            \"title\":\"企业黄页\",\n" +
                "            \"img_url\":\"https://weqxin.com/up/img/ads/appadv/20160907184727-2952889586-834015805.jpg\",\n" +
                "            \"href\":\"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"title\":\"营销工具\",\n" +
                "            \"img_url\":\"https://weqxin.com/up/img/ads/appadv/20160907184752-1390331441-1894808875.jpg\",\n" +
                "            \"href\":\"2\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"title\":\"组织架构\",\n" +
                "            \"img_url\":\"https://weqxin.com/up/img/ads/appadv/20160907184807-2055183210-1883426267.jpg\",\n" +
                "            \"href\":\"3\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"title\":\"组织1\",\n" +
                "            \"img_url\":\"http://pic55.nipic.com/file/20141208/19462408_171130083000_2.jpg\",\n" +
                "            \"href\":\"4\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"title\":\"组织2\",\n" +
                "            \"img_url\":\"http://img2.imgtn.bdimg.com/it/u=819201812,3553302270&fm=214&gp=0.jpg\",\n" +
                "            \"href\":\"5\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"title\":\"组织3\",\n" +
                "            \"img_url\":\"http://img05.tooopen.com/images/20150531/tooopen_sy_127457023651.jpg\",\n" +
                "            \"href\":\"6\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"title\":\"组织4\",\n" +
                "            \"img_url\":\"http://k73.com/up/allimg/120906/22-120Z6140234L9.jpg\",\n" +
                "            \"href\":\"7\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"title\":\"组织5\",\n" +
                "            \"img_url\":\"http://pic47.nipic.com/20140904/18981839_095218870000_2.jpg\",\n" +
                "            \"href\":\"8\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"title\":\"组织6\",\n" +
                "            \"img_url\":\"http://pic41.nipic.com/20140518/18521768_133448822000_2.jpg\",\n" +
                "            \"href\":\"9\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"title\":\"组织7\",\n" +
                "            \"img_url\":\"http://imgsrc.baidu.com/forum/pic/item/cefc1e178a82b9017ad72597738da9773912ef18.jpg\",\n" +
                "            \"href\":\"10\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"title\":\"组织8\",\n" +
                "            \"img_url\":\"http://pic18.nipic.com/20111217/7859112_162625624000_2.jpg\",\n" +
                "            \"href\":\"11\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"title\":\"组织9\",\n" +
                "            \"img_url\":\"http://pic34.nipic.com/20131021/2531170_184744142000_2.jpg\",\n" +
                "            \"href\":\"12\"\n" +
                "        }\n" +
                "    ]\n" +
                "}", type);
        List<EnterpriseHomeEntity> list = parseObject.getList();


        ImageCycleView imageCycleView = new ImageCycleView(this);
        imageCycleView.initData(list);
        imageCycleView.setOnpageitemclicklistener(this);
        ll_root.addView(imageCycleView);

        ImageCycleView imageCycleViewTWO = new ImageCycleView(this);
        imageCycleViewTWO.initData(list);
        imageCycleViewTWO.setOnpageitemclicklistener(this);
        ll_root.addView(imageCycleViewTWO);

        ImageCycleView a = new ImageCycleView(this);
        a.initData(list);
        a.setOnpageitemclicklistener(this);
        ll_root.addView(a);

        ImageCycleView b = new ImageCycleView(this);
        b.initData(list);
        b.setOnpageitemclicklistener(this);
        ll_root.addView(b);

        ImageCycleView c = new ImageCycleView(this);
        c.initData(list);
        c.setOnpageitemclicklistener(this);
        ll_root.addView(c);

        ImageCycleView d = new ImageCycleView(this);
        d.initData(list);
        d.setOnpageitemclicklistener(this);
        ll_root.addView(d);

        ImageCycleView e = new ImageCycleView(this);
        e.initData(list);
        e.setOnpageitemclicklistener(this);
        ll_root.addView(e);

        ImageCycleView f = new ImageCycleView(this);
        f.initData(list);
        f.setOnpageitemclicklistener(this);
        ll_root.addView(f);

        ImageCycleView g = new ImageCycleView(this);
        g.initData(list);
        g.setOnpageitemclicklistener(this);
        ll_root.addView(g);

        ImageCycleView h = new ImageCycleView(this);
        h.initData(list);
        h.setOnpageitemclicklistener(this);
        ll_root.addView(h);

        ImageCycleView i = new ImageCycleView(this);
        i.initData(list);
        i.setOnpageitemclicklistener(this);
        ll_root.addView(i);


        tl_content = (TabLayout) findViewById(R.id.tl_content);
        vp = (LazyViewPager) findViewById(R.id.vp);
//        //为TabLayout添加tab名称
//        for (EnterpriseHomeEntity data : list) {
//            tl_content.addTab(tl_content.newTab().setText(data.getTitle()));
//        }
        ViewPageAdapter mViewPageAdapter = new ViewPageAdapter(getSupportFragmentManager(), this, list);
//        vp.setOffscreenPageLimit(mViewPageAdapter.getCount());
        vp.setAdapter(mViewPageAdapter);
//        tl_content.setupWithViewPager(vp);
    }


    @Override
    public void onItemClick(EnterpriseHomeEntity enterprisehomeentity, int position) {
        Toast.makeText(this, enterprisehomeentity.getHref(), Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onMoreClick(int position) {
        Toast.makeText(this, position, Toast.LENGTH_SHORT).show();
    }
}
